import {defineStore} from 'pinia';
import {ElMessage} from 'element-plus';
import {requestCategory1,requestCategory2,requestCategory3} from '@/api/attr';
import type {reqCategoryType} from '@/api/attr';

export interface attrStateType{
  categoryList1:reqCategoryType,
  categoryList2:reqCategoryType,
  categoryList3:reqCategoryType,
  category1Id:null | number,
  category2Id:null | number,
  category3Id:null | number
}
const useAttrStore = defineStore('attr',{
  state:():attrStateType=>{
    return{
      categoryList1:[],
      categoryList2:[],
      categoryList3:[],
      category1Id:null,
      category2Id:null,
      category3Id:null
    }
  },
  getters:{},
  actions:{
  //  请求一及列表
    async getCategory1 (){
      try {
        const re =await requestCategory1()
        this.categoryList1 = re
      }catch (e){
        ElMessage.error(e as any)
      }
    },
    async getCategory2 (){
      try {
        const re = await requestCategory2(this.category1Id as number)
        this.categoryList2 = re
      }catch (e){
        ElMessage.error(e as any)
      }
    },
    async getCategory3 (){
      try {
        const re = await requestCategory3(this.category2Id as number)
        this.categoryList3 = re
      }catch (e){
        ElMessage.error(e as  any)
      }
    }

  }
})

export default useAttrStore